package com.blrs.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.blrs.domain.MainType;
import com.blrs.service.maintype.IMainTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;

/**
 * 主分类Controller
 * 
 * @author yuxiang.chen
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/system/type")
public class MainTypeController extends BaseController
{
    @Autowired
    private IMainTypeService mainTypeService;

    /**
     * 查询主分类列表
     */
    @GetMapping("/list")
    public AjaxResult list(MainType mainType)
    {
        List<Map> list = mainTypeService.getTree(mainType);
        return success(list);
    }

    @GetMapping("/listOther")
    public AjaxResult listOther(MainType mainType)
    {
        List<Map> list = mainTypeService.getTreeOther(mainType);
        return success(list);
    }

    /**
     * 导出主分类列表
     */
    @Log(title = "主分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MainType mainType)
    {
        List<MainType> list = mainTypeService.selectMainTypeList(mainType);
        ExcelUtil<MainType> util = new ExcelUtil<MainType>(MainType.class);
        util.exportExcel(response, list, "主分类数据");
    }

    /**
     * 获取主分类详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(mainTypeService.selectMainTypeById(id));
    }

    /**
     * 新增主分类
     */
    @Log(title = "主分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MainType mainType)
    {
        return toAjax(mainTypeService.insertMainType(mainType));
    }

    /**
     * 修改主分类
     */
    @Log(title = "主分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MainType mainType)
    {
        return toAjax(mainTypeService.updateMainType(mainType));
    }

    /**
     * 删除主分类
     */
    @Log(title = "主分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(mainTypeService.deleteMainTypeById(ids[0]));
    }
}
